package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.domain.service.IPermissionService;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.dto.ResourceTreeInfo;
import com.hyzx.qbasic.admin.model.po.RolePO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.model.vo.res.admin.GetRoleInfoRes;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.IRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2017/9/29
 */

@Api(tags = "角色管理", description = "RoleController")
@Controller
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @SysLog("角色管理首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_ROLE_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_ROLE_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_ROLE_DELETE));
        return "system/role/list";
    }

    @SysLog("获取角色列表")
    @GetMapping("/getRoleList")
    @ResponseBody
    public BaseResModel<PageInfo<RolePO>> getRoleList(@Valid GetRoleListReq req, BindingResult result) {
        return BaseResUtils.success(roleService.getPagedRoleList(req));
    }

    @SysLog("新增角色页")
    @GetMapping("/add")
    public String add() {
        return "system/role/add";
    }

    @SysLog("新增角色表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddRoleReq req, BindingResult result) {
        BaseOperationRes res = roleService.addRole(req);
        return BaseResUtils.success(res);
    }

    @SysLog("获取资源树列表")
    @GetMapping("/getResourceTreeList")
    @ResponseBody
    public BaseResModel<List<ResourceTreeInfo>> getResourceTreeList() {
        return BaseResUtils.success(permissionService.getAllResourceList());
    }

    @SysLog("删除角色")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteRoleReq req, BindingResult result) {
        BaseOperationRes res = roleService.deleteRole(req);
        return BaseResUtils.success(res);
    }

    @SysLog("编辑角色页")
    @GetMapping("/edit")
    public String edit() {
        return "system/role/edit";
    }

    @SysLog("编辑角色表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditRoleReq req, BindingResult result) {
        BaseOperationRes res = roleService.editRole(req);
        return BaseResUtils.success(res);
    }

    @SysLog("获取角色信息")
    @GetMapping("/getRoleInfo")
    @ResponseBody
    public BaseResModel<GetRoleInfoRes> getRoleInfo(@Valid GetRoleInfoReq req, BindingResult result) {
        GetRoleInfoRes res = new GetRoleInfoRes();
        res.setRoleInfo(roleService.getRoleInfo(req.getRoleId()));
        res.setResourceList(roleService.getRoleResources(req.getRoleId()));

        return BaseResUtils.success(res);
    }

    @SysLog("获取全部角色列表")
    @GetMapping("/getAllRoleList")
    @ResponseBody
    public BaseResModel<List<RolePO>> getAllRoleList() {
        return BaseResUtils.success(roleService.getAllRoleList());
    }
}
